package com.lsmo.android.carfans.activity.news;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.lsmo.android.carfans.R;
import com.lsmo.android.carfans.model.news.News;

import android.R.integer;
import android.content.Context;
import android.content.Intent;
import android.graphics.Color;
import android.util.Log;
import android.view.Gravity;
import android.view.View;
import android.view.ViewGroup.LayoutParams;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.SimpleAdapter;
import android.widget.TextView;
import android.widget.Toast;

/**
 * 新闻数据适配器
 * 
 * @author Administrator
 * 
 */
public class NewsAdapter {
	private static int index;
	public static int getIndex() {
		return index;
	}

	private Context context;
	private static Map<String, News> newsFactory;//装载新闻信息，每次请求时应清空
	public static Map<String, News> getNewsFactory() {
		return newsFactory;
	}

	public static void setNewsFactory(Map<String, News> newsFactory) {
		NewsAdapter.newsFactory = newsFactory;
	}

	public NewsAdapter(Context context, LinearLayout layout, News... news) {
		newsFactory=new HashMap<String, News>();
		this.context = context;
		initLayout(layout,news);
		
	}

	private void initLayout(LinearLayout layout,News... news) {
		layout.setBackgroundColor(Color.WHITE);
		for (int i = 0; i < news.length; i++) {
			newsFactory.put(String.valueOf(i),news[i]);
			
			LinearLayout itemLayout = new LinearLayout(context);
			itemLayout.setId(i);
			itemLayout.setLayoutParams(new LayoutParams(LayoutParams.FILL_PARENT,
					90));
			itemLayout.setGravity(Gravity.CENTER_VERTICAL);
			
			itemLayout.setPadding(20, 0, 0, 0);
			itemLayout.setOrientation(LinearLayout.HORIZONTAL);
			ImageView img=new ImageView(context);
			img.setImageResource(news[i].getImgId());
			itemLayout.addView(img);
			
			LinearLayout infoLayout=new LinearLayout(context);
			infoLayout.setOrientation(LinearLayout.VERTICAL);
			infoLayout.setPadding(20, 0, 0, 0);
			TextView txtTitle=new TextView(context);

			txtTitle.setText(news[i].getTitle());
			txtTitle.setTextColor(Color.BLACK);
			txtTitle.setTextSize(15);
			infoLayout.addView(txtTitle);
			
			TextView txtSum=new TextView(context);
			txtSum.setText(news[i].getSummary());
			txtSum.setTextColor(Color.GRAY);
			txtSum.setTextSize(12);
			infoLayout.addView(txtSum);
			
			itemLayout.addView(infoLayout);
			
			itemLayout.setOnClickListener(new View.OnClickListener() {
				
				@Override
				public void onClick(View v) {
					index=v.getId();
					NewsActivity.setViewTemp(v);
//					Toast.makeText(context, String.valueOf(i), Toast.LENGTH_SHORT).show();
					v.setBackgroundResource(R.drawable.news_main_layout_item_selected);
				
					Intent i=new Intent(context, NewsDetailActivity.class);
					context.startActivity(i);
					
				}
			}) ;
			
			
			layout.addView(itemLayout);
			
			View lines = new View(context);
			 lines.setLayoutParams(new LayoutParams(
			 LayoutParams.FILL_PARENT, 2));
			 lines.setBackgroundColor(Color.GRAY);
			 layout.addView(lines);
			 
		}
	}

}
